<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 目标和 -->
    <script>
      var findTargetSumWays = function (nums, target) {
        const sum = nums.reduce((p, v) => p + v)
        if (Math.abs(target) > sum) return 0
        if ((target + sum) % 2) return 0
        const left = (target + sum) / 2
        let dp = new Array(left + 1).fill(0)
        dp[0] = 1
        for (let i = 0; i < nums.length; i++) {
          for (let j = left; j >= nums[i]; j--) {
            dp[j] += dp[j - nums[i]]
          }
        }
        return dp[left]
      }
      console.log(findTargetSumWays([1, 1, 1, 1, 1], 3))
    </script>
  </body>
</html>
